package com.lin.jedis;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;

import java.util.Map;

import static java.util.Map.entry;

public class JedisTest {
    private Jedis jedis;

    @BeforeEach
    void setUp() {
        // 连接redis
        jedis = new Jedis("192.168.18.100", 6379);
        // 设置密码
        jedis.auth("123321");
        // 选择数据库(可选)
        jedis.select(0);
    }

    @Test
    void testHash() {
        // 设置多个键值对
        Map<String, String> map = Map.ofEntries(
                entry("name", "zhangSan"),
                entry("age", "18"),
                entry("gender", "male")
        );
        Map<String, String> map1 = Map.of(
                "name", "zhangSan1",
                "age", "18",
                "gender", "male"
        );
        jedis.hmset("user", map);
        jedis.hmset("user1", map1);

        System.out.println(jedis.hgetAll("user"));
        System.out.println(jedis.hgetAll("user1"));
    }

    @AfterEach
    void tearDown() {
        // 关闭连接
        if (jedis != null) jedis.close();
    }
}
